#include <sys/epoll.h>

#include "Channel.h"
#include "EventLoop.h"


const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = EPOLLIN | EPOLLPRI;
const int Channel::kWriteEvent = EPOLLOUT;

Channel::Channel(EventLoop* loop, int fd) :
    loop_(loop),
    fd_(fd),
    events_(0),
    revents_(0),
    state_(NEW) 
{


}

void Channel::update() {
    loop_->updateChannel(this);
}

void Channel::handleEvent() {
    if ((revents_ & EPOLLIN) || (revents_ & EPOLLRDHUP) || (revents_ & EPOLLPRI)) {
        readCallback_();
    } else if (revents_ & EPOLLOUT) {
        writeCallback_();
    } else if (revents_ & EPOLLERR) {
        errorCallback_();
    }
}

